package com.kirk.cts.core.loader;

import com.kirk.cts.core.constants.SystemConstants;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;


/**
 * <p> LanguageLoader.java </p>
 *
 * @version 1.0.0
 * @Author kirkliu
 * @Date 2020-08-29 22:23
 * @Description 国际化工具类
 */
public class LanguageLoader {
    /**
     * 语言资源绑定对象
     */
    private static ResourceBundle languageBundle;

    /**
     * 选择的语言
     */
    private static Locale languageSelected;

    /**
     * ClassLoader
     */
    private static final ClassLoader WD_CLASS_LOADER;

    static {
        WD_CLASS_LOADER = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
            @Override
            public ClassLoader run() {
                return new ClassLoader() {
                    @Override
                    public URL getResource(String name) {
                        try {
                            URL result = super.getResource(name);
                            if (result != null) {
                                return result;
                            }
                            return (new File(name)).toURI().toURL();
                        } catch (MalformedURLException ex) {
                            return null;
                        }
                    }
                };
            }
        });
    }

    /**
     * 设置本地化语言
     */
    public static void setLanguage(Locale locale) {
        if (locale == null) {
            languageBundle = ResourceBundle.getBundle(SystemConstants.I18N_BUNDLE_NAME, Locale.getDefault(), WD_CLASS_LOADER);
        } else {
            languageBundle = ResourceBundle.getBundle(SystemConstants.I18N_BUNDLE_NAME, locale, WD_CLASS_LOADER);
        }
        languageSelected = languageBundle.getLocale();
    }

    /**
     * 取得选择的语言
     */
    public static Locale getLanguageSelected() {
        return languageSelected;
    }

    /**
     * 根据键，取得值
     */
    public static String getString(String key) {
        if (languageBundle != null) {
            String result;
            try {
                result = languageBundle.getString(key);
            } catch (MissingResourceException e) {
                return key;
            }
            return result;
        }
        return key;
    }
}
